/*=============================================================================
#    The element-comm is a tentative program used to test our ideas.           
#                       Copyright (C)  bgao                                    
#
#     This program is free software; you can redistribute it and/or            
#      modify it under the terms of the GNU General Public License             
#     as published by the Free Software Foundation; either version 2           
#         of the License, or (at your option) any later version.               
#
#     This program is distributed in the hope that it will be useful,          
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            
#             GNU General Public License for more details.                    
#
#    You should have received a copy of the GNU General Public License        
#     along with this program; if not, write to the Free Software             
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             
#                       MA  02110-1301, USA.                                  
#==============================================================================
# $Id: mac.c 348 2010-11-29 09:01:16Z bgao $ 
#
# $Author: bgao $ 
#
# $Revision: 348 $ 
#
# $Date: 2010-11-29 17:01:16 +0800 (星期一, 29 十一月 2010) $ 
#
# Description: 
#
=============================================================================*/
/*! @file mac.c
 *  @brief This file mainly provide a initialization method
           mac_init for the MAC layer.
 */
#include "mac.h"

/*! initialize the MAC layer.
 *  @return return the result of this operation.
 */
int8 mac_init(void)
{
    uint64 extended_addr = (timer_get() % 0xffffffff);
    mac_set_extended_addr(extended_addr);
    rf_set_ieee_addr(extended_addr);

    rf_init();
    phy_init();
    mac_info_init();
    buf_init();
    mac_indir_init();
    mac_retry_init();
    mac_queue_init();
    /* timer init needed when running with os */
    return MAC_OK;
}

/*! provide a soft-acknowledgment-generation method, this functionality
 *  is provided by the most RF hardware.
 *  @param dsn the data sequence to acknowledged
 *  @return return the result this operation
 */ 
int8 mac_ack(uint8 dsn)
{
    buf_t *buf;
    mac_frm_ack_t ack;

    buf = buf_alloc(true);
    if (buf == NULL) {
        return MAC_ERR;
    }

    ack.dsn = dsn;
    /* TODO: check the neighbor table to see if there're data pending for the node */
    ack.pend = 0;
    mac_gen_ack(buf, &ack);
    mac_csmaca_tx(buf, dsn, 0);

    return MAC_OK;
}

